home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / glibc-1.09 / glibc-1 / glibc-1.09.1 / time / test_time.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-16  |  2.6 KB  |  118 lines

  1. /* Copyright (C) 1991, 1992, 1994 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8.  
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. #include <ansidecl.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <time.h>
  23.  
  24.  
  25. int
  26. DEFUN(main, (argc, argv), int argc AND char **argv)
  27. {
  28.   time_t t;
  29.   register struct tm *tp;
  30.   struct tm tbuf;
  31.   int lose = 0;
  32.  
  33.   --argc;
  34.   ++argv;
  35.  
  36.   do
  37.     {
  38.       char buf[BUFSIZ];
  39.       if (argc > 0)
  40.     {
  41.       static char buf[BUFSIZ];
  42.       sprintf(buf, "TZ=%s", *argv);
  43.       if (putenv(buf))
  44.         {
  45.           puts("putenv failed.");
  46.           lose = 1;
  47.         }
  48.       else
  49.         puts (buf);
  50.     }
  51.       tzset();
  52.       tbuf.tm_year = 72;
  53.       tbuf.tm_mon = 0;
  54.       tbuf.tm_mday = 31;
  55.       tbuf.tm_hour = 6;
  56.       tbuf.tm_min = 14;
  57.       tbuf.tm_sec = 50;
  58.       tbuf.tm_isdst = -1;
  59.     doit:;
  60.       t = mktime(&tbuf);
  61.       if (t == (time_t) -1)
  62.     {
  63.       puts("mktime() failed?");
  64.       lose = 1;
  65.     }
  66.       tp = localtime(&t);
  67.       if (tp == NULL)
  68.     {
  69.       puts("localtime() failed.");
  70.       lose = 1;
  71.     }
  72.       else if (strftime(buf, sizeof(buf), "%a %b %d %X %Z %Y", tp) == 0)
  73.     {
  74.       puts("strftime() failed.");
  75.       lose = 1;
  76.     }
  77.       else
  78.     puts(buf);
  79.       if (tbuf.tm_year == 101)
  80.     {
  81.       tbuf.tm_year = 97;
  82.       tbuf.tm_mon = 0;
  83.       goto doit;
  84.     }
  85.       ++argv;
  86.     } while (--argc > 0);
  87.  
  88.   {
  89. #define    SIZE    256
  90.     char buffer[SIZE];
  91.     time_t curtime;
  92.     struct tm *loctime;
  93.  
  94.     curtime = time (NULL);
  95.  
  96.     loctime = localtime (&curtime);
  97.  
  98.     fputs (asctime (loctime), stdout);
  99.  
  100.     strftime (buffer, SIZE, "Today is %A, %B %d.\n", loctime);
  101.     fputs (buffer, stdout);
  102.     strftime (buffer, SIZE, "The time is %I:%M %p.\n", loctime);
  103.     fputs (buffer, stdout);
  104.  
  105.     loctime->tm_year = 72;
  106.     loctime->tm_mon = 8;
  107.     loctime->tm_mday = 12;
  108.     loctime->tm_hour = 20;
  109.     loctime->tm_min = 49;
  110.     loctime->tm_sec = 05;
  111.     curtime = mktime (loctime);
  112.     strftime (buffer, SIZE, "%D %T was %w the %jth.\n", loctime);
  113.     fputs (buffer, stdout);
  114.   }
  115.  
  116.   return (lose ? EXIT_FAILURE : EXIT_SUCCESS);
  117. }
  118.